/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package malateproject;

import DBM.DBManager;
import javax.swing.JOptionPane;

/**
 *
 * @author Alexander
 */
public class Register extends javax.swing.JFrame {

    /**
     * Creates new form Register
     */
    public Register() {
        initComponents();
        this.setVisible(true);
                jLabel17.setVisible(false);       
                jLabel18.setVisible(false);          
                jLabel19.hide();           
                jLabel20.hide();         
                jLabel21.hide();          
                jLabel22.hide();          
                jLabel26.hide();          
                jLabel27.hide();           
                jLabel28.hide();           
                jLabel23.hide();         
                jLabel24.hide();
                jLabel29.hide();
    }
    
    private Objects.Member copyFromFields() {
        Objects.Member newMember = new Objects.Member();

        newMember.setFirstName(jTextFieldIDnum.getText());
        newMember.setMiddleName(jTextFieldFname.getText());
        newMember.setLastName(jTextFieldMname.getText());
        newMember.setIdNo(jTextFieldIDnum.getText());
        newMember.setFirstName(jTextFieldFname.getText());
        newMember.setMiddleName(jTextFieldMname.getText());
        newMember.setLastName(jTextFieldLname.getText());
        newMember.setSection(jTextFieldSection.getText());
        newMember.setStatus(jTextFieldStatus.getText());
        newMember.setCollege(jComboBoxColl.getSelectedItem().toString());
        newMember.setCourse(jTextFieldCourse.getText());
        newMember.setContactNo(jTextFieldContactNo.getText());
        newMember.setEmailAd(jTextFieldEmail.getText());
        newMember.setDateOfBirth(jTextFieldDOB.getText());
        
        
        
        return newMember;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTextFieldIDnum = new javax.swing.JTextField();
        jTextFieldFname = new javax.swing.JTextField();
        jTextFieldMname = new javax.swing.JTextField();
        jTextFieldLname = new javax.swing.JTextField();
        jTextFieldSection = new javax.swing.JTextField();
        jTextFieldStatus = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jTextFieldCourse = new javax.swing.JTextField();
        jTextFieldContactNo = new javax.swing.JTextField();
        jTextFieldEmail = new javax.swing.JTextField();
        jTextFieldDOB = new javax.swing.JTextField();
        jButtonBack = new javax.swing.JButton();
        jButtonRegister = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jPassword = new javax.swing.JPasswordField();
        jRetypePassword = new javax.swing.JPasswordField();
        jLabel16 = new javax.swing.JLabel();
        jComboBoxColl = new javax.swing.JComboBox();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel26 = new javax.swing.JLabel();
        jLabel27 = new javax.swing.JLabel();
        jLabel28 = new javax.swing.JLabel();
        jLabel29 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jLabel2.setFont(new java.awt.Font("Calibri", 1, 18)); // NOI18N
        jLabel2.setText("Registration Page");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(160, 100, 140, 20);

        jLabel3.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("ID Number:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(40, 200, 70, 20);

        jLabel4.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Lastname:");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(50, 350, 60, 20);

        jLabel5.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Section:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(60, 400, 50, 20);

        jLabel6.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Status:");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(60, 450, 40, 20);

        jLabel7.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("Middlename:");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(30, 300, 90, 20);

        jLabel8.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("Firstname:");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(50, 250, 70, 20);
        getContentPane().add(jTextFieldIDnum);
        jTextFieldIDnum.setBounds(110, 200, 210, 30);
        getContentPane().add(jTextFieldFname);
        jTextFieldFname.setBounds(110, 250, 210, 30);
        getContentPane().add(jTextFieldMname);
        jTextFieldMname.setBounds(110, 300, 210, 30);
        getContentPane().add(jTextFieldLname);
        jTextFieldLname.setBounds(110, 350, 210, 30);
        getContentPane().add(jTextFieldSection);
        jTextFieldSection.setBounds(110, 400, 210, 30);
        getContentPane().add(jTextFieldStatus);
        jTextFieldStatus.setBounds(110, 450, 210, 30);

        jLabel9.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(255, 255, 255));
        jLabel9.setText("College:");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(450, 290, 50, 16);

        jLabel10.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(255, 255, 255));
        jLabel10.setText("Course:");
        getContentPane().add(jLabel10);
        jLabel10.setBounds(450, 340, 50, 16);

        jLabel11.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel11.setForeground(new java.awt.Color(255, 255, 255));
        jLabel11.setText("Contact No.:");
        getContentPane().add(jLabel11);
        jLabel11.setBounds(430, 390, 70, 16);

        jLabel12.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel12.setForeground(new java.awt.Color(255, 255, 255));
        jLabel12.setText("Email:");
        getContentPane().add(jLabel12);
        jLabel12.setBounds(460, 440, 40, 16);

        jLabel13.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel13.setForeground(new java.awt.Color(255, 255, 255));
        jLabel13.setText("Date of Birth:");
        getContentPane().add(jLabel13);
        jLabel13.setBounds(420, 490, 80, 16);
        getContentPane().add(jTextFieldCourse);
        jTextFieldCourse.setBounds(500, 330, 210, 30);
        getContentPane().add(jTextFieldContactNo);
        jTextFieldContactNo.setBounds(500, 380, 210, 30);
        getContentPane().add(jTextFieldEmail);
        jTextFieldEmail.setBounds(500, 430, 210, 30);
        getContentPane().add(jTextFieldDOB);
        jTextFieldDOB.setBounds(500, 480, 210, 30);

        jButtonBack.setText("Back");
        jButtonBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBackActionPerformed(evt);
            }
        });
        getContentPane().add(jButtonBack);
        jButtonBack.setBounds(680, 550, 90, 30);

        jButtonRegister.setText("Register");
        jButtonRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegisterActionPerformed(evt);
            }
        });
        getContentPane().add(jButtonRegister);
        jButtonRegister.setBounds(570, 550, 90, 30);

        jLabel14.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel14.setForeground(new java.awt.Color(255, 255, 255));
        jLabel14.setText("Password:");
        getContentPane().add(jLabel14);
        jLabel14.setBounds(430, 200, 70, 14);

        jLabel15.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel15.setForeground(new java.awt.Color(255, 255, 255));
        jLabel15.setText("Retype Password:");
        getContentPane().add(jLabel15);
        jLabel15.setBounds(390, 240, 95, 16);
        getContentPane().add(jPassword);
        jPassword.setBounds(500, 180, 210, 30);
        getContentPane().add(jRetypePassword);
        jRetypePassword.setBounds(500, 230, 210, 30);

        jLabel16.setText("jLabel16");
        getContentPane().add(jLabel16);
        jLabel16.setBounds(440, 240, 40, 14);

        jComboBoxColl.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CCS", "COB", "CED", "COE", "COS", "CLA" }));
        jComboBoxColl.setSelectedIndex(-1);
        getContentPane().add(jComboBoxColl);
        jComboBoxColl.setBounds(500, 280, 210, 30);

        jLabel17.setForeground(new java.awt.Color(255, 0, 0));
        jLabel17.setText("X");
        getContentPane().add(jLabel17);
        jLabel17.setBounds(330, 210, 30, 14);

        jLabel18.setForeground(new java.awt.Color(255, 0, 0));
        jLabel18.setText("X");
        getContentPane().add(jLabel18);
        jLabel18.setBounds(330, 260, 40, 14);

        jLabel19.setForeground(new java.awt.Color(255, 0, 0));
        jLabel19.setText("X");
        getContentPane().add(jLabel19);
        jLabel19.setBounds(330, 310, 50, 14);

        jLabel20.setForeground(new java.awt.Color(255, 0, 0));
        jLabel20.setText("X");
        getContentPane().add(jLabel20);
        jLabel20.setBounds(330, 360, 50, 14);

        jLabel21.setForeground(new java.awt.Color(255, 0, 0));
        jLabel21.setText("X");
        getContentPane().add(jLabel21);
        jLabel21.setBounds(330, 410, 50, 14);

        jLabel22.setForeground(new java.awt.Color(255, 0, 0));
        jLabel22.setText("X");
        getContentPane().add(jLabel22);
        jLabel22.setBounds(330, 460, 50, 14);

        jLabel23.setForeground(new java.awt.Color(255, 0, 0));
        jLabel23.setText("X");
        getContentPane().add(jLabel23);
        jLabel23.setBounds(720, 190, 50, 14);

        jLabel24.setForeground(new java.awt.Color(255, 0, 0));
        jLabel24.setText("X");
        getContentPane().add(jLabel24);
        jLabel24.setBounds(720, 240, 40, 14);

        jLabel26.setForeground(new java.awt.Color(255, 0, 0));
        jLabel26.setText("X");
        getContentPane().add(jLabel26);
        jLabel26.setBounds(720, 340, 50, 14);

        jLabel27.setForeground(new java.awt.Color(255, 0, 0));
        jLabel27.setText("X");
        getContentPane().add(jLabel27);
        jLabel27.setBounds(720, 390, 60, 14);

        jLabel28.setForeground(new java.awt.Color(255, 0, 0));
        jLabel28.setText("X");
        getContentPane().add(jLabel28);
        jLabel28.setBounds(720, 440, 60, 14);

        jLabel29.setForeground(new java.awt.Color(255, 0, 0));
        jLabel29.setText("X");
        getContentPane().add(jLabel29);
        jLabel29.setBounds(720, 490, 50, 14);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/MalateBG.E-res.jpg"))); // NOI18N
        jLabel1.setText("jLabel1");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(0, 0, 800, 600);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBackActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        new FrontPage().setVisible(true);
          
    }//GEN-LAST:event_jButtonBackActionPerformed

    private void jButtonRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegisterActionPerformed
        // TODO add your handling code here:
        //check if there are empty fields
       
        if(jTextFieldIDnum.getText().isEmpty() || jTextFieldFname.getText().isEmpty() ||
           jTextFieldMname.getText().isEmpty() || jTextFieldLname.getText().isEmpty() ||
           jTextFieldSection.getText().isEmpty() || jTextFieldStatus.getText().isEmpty() ||
            jTextFieldCourse.getText().isEmpty() ||
           jTextFieldContactNo.getText().isEmpty()  || jTextFieldEmail.getText().isEmpty() || 
           jPassword.getText().isEmpty() || jRetypePassword.getText().isEmpty() 
           && jPassword.getText().equals(jRetypePassword.getText()) || jTextFieldDOB.getText().isEmpty() )
        {
            //check each field to show X
            if(jTextFieldIDnum.getText().isEmpty())
                jLabel17.setVisible(true);
            if(jTextFieldFname.getText().isEmpty())
                jLabel18.setVisible(true);
            if(jTextFieldLname.getText().isEmpty())
                jLabel19.setVisible(true);
            if(jTextFieldLname.getText().isEmpty())
                jLabel20.setVisible(true);
            if(jTextFieldSection.getText().isEmpty())
                jLabel21.setVisible(true);
            if(jTextFieldStatus.getText().isEmpty())
                jLabel22.setVisible(true);
            if(jTextFieldCourse.getText().isEmpty())
                jLabel26.setVisible(true);
            if(jTextFieldContactNo.getText().isEmpty())
                jLabel27.setVisible(true);
            if(jTextFieldEmail.getText().isEmpty())
                jLabel28.setVisible(true);
            if(jPassword.getText().isEmpty() || jPassword.getText().equals(jRetypePassword.getText()))
                jLabel23.setVisible(true);
            if(jTextFieldDOB.getText().isEmpty())
                jLabel29.setVisible(true);
            if(jRetypePassword.getText().isEmpty() || jPassword.getText().equals(jRetypePassword.getText()))
                jLabel24.setVisible(true);
            
            
            JOptionPane.showMessageDialog(null, "Please fill in all the required fields.");
        }
        else
        {
            jLabel17.setVisible(false);       
             jLabel18.setVisible(false);          
                jLabel19.hide();           
                jLabel20.hide();         
                jLabel21.hide();          
                jLabel22.hide();          
                jLabel26.hide();          
                jLabel27.hide();           
                jLabel28.hide();           
                jLabel23.hide();         
                jLabel24.hide();
                jLabel29.hide();
            
            
        //database properties
           DBM.DBManager dm = new DBM.DBManager();
           
           Objects.Member newMember = copyFromFields();
           
             if (!dm.memberExists(newMember)) {
                    newMember = copyFromFields();
                    if (dm.addMember(newMember)) {
                        JOptionPane.showMessageDialog(null, "Registration Successful",
                            "Successful", JOptionPane.INFORMATION_MESSAGE);
                        
                    }
                }
                else {
                    JOptionPane.showMessageDialog(null, "Username already exists",
                            "Error", JOptionPane.ERROR_MESSAGE);
                }
            
            
        }
        
       
    }//GEN-LAST:event_jButtonRegisterActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Register().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBack;
    private javax.swing.JButton jButtonRegister;
    private javax.swing.JComboBox jComboBoxColl;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPasswordField jPassword;
    private javax.swing.JPasswordField jRetypePassword;
    private javax.swing.JTextField jTextFieldContactNo;
    private javax.swing.JTextField jTextFieldCourse;
    private javax.swing.JTextField jTextFieldDOB;
    private javax.swing.JTextField jTextFieldEmail;
    private javax.swing.JTextField jTextFieldFname;
    private javax.swing.JTextField jTextFieldIDnum;
    private javax.swing.JTextField jTextFieldLname;
    private javax.swing.JTextField jTextFieldMname;
    private javax.swing.JTextField jTextFieldSection;
    private javax.swing.JTextField jTextFieldStatus;
    // End of variables declaration//GEN-END:variables
}
